import { salaryListUrl } from '../../../../api/individual'
import { post, formatDate } from '../../../../utils/index'

Page({
  data: {
    list: [],
    params: {
      current: 1,
      size: 10,
      startDate: formatDate(new Date(new Date().getFullYear().toString()), 'yyyyMM'),
      endDate: formatDate(new Date(), 'yyyyMM'),
    },
    finished: false,
    bgList: ['blue-color-bg', 'yellow-color-bg', 'green-color-bg', 'pink-color-bg', 'purple-color-bg'],
    startDatePopup: false,
    endDatePopup: false,
    startDateTitle: formatDate(new Date(new Date().getFullYear().toString()), 'yyyy年MM月'),
    endDateTitle: formatDate(new Date(), 'yyyy年MM月'),
    startDate: new Date(new Date().getFullYear().toString()).getTime(),
    endDate: new Date(`${new Date().getFullYear()}-${new Date().getMonth() + 1}`).getTime(),
    maxStartDate: new Date(`${new Date().getFullYear()}-${new Date().getMonth() + 1}`).getTime(),
    minEndDate: new Date(new Date().getFullYear().toString()).getTime(),
    maxDate: new Date().getTime(),
    formatter (type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      return `${value}月`
    }
  },
  async loadData () { // 获取工资条
    let { params, finished } = this.data
    if (finished) {
      return
    }
    let { data: { rows, total } } = await post(salaryListUrl, params)
    rows.forEach(item => {
      item.date = `${item.fullMonth.slice(0, 4)}年${item.fullMonth.slice(4)}月`
      item.timestamp = new Date(`${item.fullMonth.slice(0, 4)}-${item.fullMonth.slice(4)}`).getTime()
    })
    let { list } = this.data
    list = list.concat(rows)
    params.current++
    if (list.length >= total) {
      finished = true
    }
    this.setData({ list, params, finished })
  },
  onPullDownRefresh () { // 上拉刷新
    const { params } = this.data
    params.current = 1
    this.setData({ list: [], finished: false, params })
    this.loadData()
  },
  onReachBottom () { // 下拉加载更多
    this.loadData()
  },
  salary ({ currentTarget }) { // 查看详情
    const { index } = currentTarget.dataset
    let timestamp = this.data.list[index].timestamp
    wx.navigateTo({ url: `/pages/individual/work/salary/salary?timestamp=${timestamp}` })
  },
  startDateClick () { // 开始时间弹窗
    this.setData({ startDatePopup: true })
  },
  startDateChange ({ detail }) { // 选择开始时间日期
    const { params } = this.data
    const startDateTitle = formatDate(new Date(detail), 'yyyy年MM月')
    const startDate = formatDate(new Date(detail), 'yyyyMM')
    params.startDate = startDate
    this.setData({ params, startDateTitle, startDate: detail, minEndDate: detail })
    this.loadData()
    this.startDateClose()
  },
  startDateClose () { // 关闭开始时间
    this.setData({ startDatePopup: false })
  },
  endDateClick () { // 结束时间弹窗
    this.setData({ endDatePopup: true })
  },
  endDateChange ({ detail }) { // 选择结束时间日期
    const { params } = this.data
    const endDateTitle = formatDate(new Date(detail), 'yyyy年MM月')
    const endDate = formatDate(new Date(detail), 'yyyyMM')
    params.endDate = endDate
    this.setData({ endDateTitle, params, endDate: detail, maxStartDate: detail })
    this.loadData()
    this.endDateClose()
  },
  endDateClose () { // 关闭结束时间
    this.setData({ endDatePopup: false })
  },
  onLoad () {
    this.loadData()
  }
})